from fastapi import Depends, FastAPI
from extensions.db.mysql import create_db_and_tables
from application.ucenter.user.schema import UserCreate, UserRead, UserUpdate
from config.config import Settings, get_settings
from application.ucenter.model import User
from common.auth.user import auth_backend, current_active_user, fastapi_users
from application.ucenter.role.view import role_router
from application.ucenter.user.view import user_roters
from application.ucenter.power.view import power_router
from typing import Annotated

app = FastAPI()

app.include_router(
    fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"]
)
app.include_router(
    fastapi_users.get_register_router(UserRead, UserCreate),
    prefix="/auth",
    tags=["auth"],
)
app.include_router(
    fastapi_users.get_reset_password_router(),
    prefix="/auth",
    tags=["auth"],
)
app.include_router(
    fastapi_users.get_verify_router(UserRead),
    prefix="/auth",
    tags=["auth"],
)
app.include_router(
    fastapi_users.get_users_router(UserRead, UserUpdate),
    prefix="/users",
    tags=["users"],
)
app.include_router(
    user_roters,
    prefix="/user",
    tags=["user"],
)
app.include_router(
    role_router,
    prefix="/role",
    tags=["role"],
)
app.include_router(
    power_router,
    prefix="/power",
    tags=["power"],
)


@app.get("/authenticated-route")
async def authenticated_route(user: User = Depends(current_active_user)):
    return {"message": f"Hello {user.email}!"}


@app.get("/setting")
async def get_settings(settings: Annotated[Settings, Depends(get_settings)]):
    """
    annotated表示:
    类型是Settings
    添加元数据get_settings()
    """
    return {
        "app_name": settings.app_name,
        "mysql_url": settings.database_url,
    }


@app.on_event("startup")
async def on_startup():
    # Not needed if you setup a migration system like Alembic
    await create_db_and_tables()
